/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package connection;


import java.net.*;
import java.io.*;
import java.util.*;
import overlay.*;
import protocollo.*;
import java.util.concurrent.*;



public class Connection {

    private int id;
    private Server server;
    private Vector<Client> clients;
    private static final int BACKLOG = 5;
    private LinkedBlockingQueue coda;


    public Connection(LinkedBlockingQueue coda) throws Exception{
        this.coda = coda;
        server = new Server(coda, BACKLOG);
        
        server.start();
        clients = new Vector<Client>();
    }

    public void aggiungiPeer(String ip, int port) throws Exception {

       Client client = new Client(ip, port);
       clients.add(client);
    }

    public int getPortServer(){
        return server.getPort();
    }


    public void sendMessage(Pacchetto p) throws Exception{
       
            for(int j=0; j<clients.size(); j++) {
                clients.elementAt(j).sendPacchetto(p);
            }

    }


    public void close() throws Exception{
        server.closeSocket();
        
    }


}
